﻿<%@ Page Title="" Language="C#"  AutoEventWireup="true"  CodeBehind="ShowRoutes.aspx.cs" Inherits="GPSTrackingWebsiteManager.Pages.ShowRoutes" %>


<!DOCTYPE html>

<link href="../../Css/GPSTrackingWebsite.css" rel="stylesheet" />
<link href="../../Css/messi.css" rel="stylesheet" />
<script src="../../Javascript/jquery-1.11.1.min.js"></script>
<script src="../../Javascript/messi.js"></script>
<script src="../../Javascript/Custom.js"></script>
<script src="../../Javascript/WindowMessageManager.js"></script>
<script src="../../Javascript/ObjectsV2.js"></script>
<script src="../../Javascript/MapManagerV2.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">  </script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Visor de rutas</title>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
    <script type="text/javascript">

        var opener = GetRadWindow();
        var GPSImeiByBusRoutesList = [];
        var GPSImei = new GPSImei();
        var MasterPage = new MasterPage();
        var intervalGPSImei;
        var map;

        var localDisplayMap = {
            firstBusRoute: true,
            companyID: null,
            busRouteID: null,
            busStopsList: [],
            busRoutesList: [],
            companiesList: [],
            companiesDataSource: [],
            temporalyIndex: -1,
            temporalyInterval: null,
            timeToCalculateRoute: 500,
            temporalyList: [],
            polygonoBusRouteMarkers: [],
            originPoint: null,
            destinationPoint: null,
            directionsService: null,
            directionsDisplay: null,
            route: null,
            busRoutes: [],
            response: null
        }

        var BusStopsManager = new BusStopsManager();

        jQuery(document).ready(function () {
            try {
                jQuery.noConflict();
            } catch (e) { };
        });

        function GetRadWindow() {
            var oWindow = null;
            if (window.radWindow)
                oWindow = window.radWindow;
            else if (window.frameElement.radWindow)
                oWindow = window.frameElement.radWindow;
            return oWindow.BrowserWindow;
        }

        window.onload = function () {
            //Windows Message Manager 
            var objWindowsMessageManager = new WindowsMessageManager();
            objWindowsMessageManager.run();

            var myLatLng = new google.maps.LatLng(10.060727, -84.43785);

            var myOptions = {
                center: myLatLng,
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                zoomControlOptions: {
                    position: google.maps.ControlPosition.RIGHT_TOP
                },
                panControlOptions: {
                    position: google.maps.ControlPosition.RIGHT_TOP
                }
            };

            this.map = new google.maps.Map(document.getElementById("dvMap"), myOptions);

            initializeRoutesObjects();

            localDisplayMap.companyID = opener.globalDisplayMap.companyID;
            localDisplayMap.busRouteID = opener.globalDisplayMap.busRouteID;
            localDisplayMap.busRoutesList = opener.globalDisplayMap.busRoutesList;
            localDisplayMap.busStopsList = opener.globalDisplayMap.busStopsList;

            showAllBusStopsChecked(localDisplayMap.busStopsList);
            GetAllBusRouteDetailsByBusRouteID();
            getGroupsTrackDataLastLocationByCompanyIDAndBusRouteID();

        }

        function getGroupsTrackDataLastLocationByCompanyIDAndBusRouteID() {
            PageMethods.wsGetImeiTrackDataLastLocationByCompanyIDAndBusRouteID(opener.globalDisplayMap.companyID, opener.globalDisplayMap.busRouteID, getImeiTrackDataLastLocationByCompanyIDAndBusRouteIDOk, getImeiTrackDataLastLocationByCompanyIDAndBusRouteIDError);
        }

        function getImeiTrackDataLastLocationByCompanyIDAndBusRouteIDOk(xmlObject) {
            //requestStart(getrpvGPSImeiUpdateControl());
            GPSImei.openXMLTrackDataLastLocationByBusRoutes(xmlObject, opener, GPSImeiByBusRoutesList, this.map);

            intervalGPSImei = setInterval("getImeiDataTrackLastLocationByBusRoutes()", 2000);
            //setTimeout(loadGPSImeiTreeWait, 10);
        }

        function getImeiTrackDataLastLocationByCompanyIDAndBusRouteIDError(error) {
            opener.objWindowsMessageManager.show_Error(error.get_message(), titles.polygonoTitle);
        }

        function getImeiDataTrackLastLocationByBusRoutes() {
            PageMethods.wsGetImeiTrackDataLastLocationByCompanyIDAndBusRouteID(opener.globalDisplayMap.companyID, opener.globalDisplayMap.busRouteID, getImeiDataTrackLastLocationByBusRoutesOk, getImeiDataTrackLastLocationByBusRoutesError);
        }

        function getImeiDataTrackLastLocationByBusRoutesOk(xmlObject) {

            MasterPage.openXMLTrackDataLastLocationImeiRoutes(opener.objMapControl, xmlObject, GPSImeiByBusRoutesList, this.map);
            //var index = MasterPage.getIndexImei(globalMasterPage.trackDataLastLocationList, globalGPSImei.imeiSelected);
            //if (index != -1) {
            //    setInfoReal(index);
            //}

            if (intervalGPSImei != null) {
                clearInterval(intervalGPSImei);
                intervalGPSImei = null;
                setInterval("getImeiDataTrackLastLocationByBusRoutes()", 5000);
            }

        }

        function getImeiDataTrackLastLocationByBusRoutesError(error) {
            //objWindowsMessageManager.show_Error(error.get_message(), titles.polygonoTitle);
        }


        function showAllBusStopsChecked(list) {
            for (lk = 0; lk < list.length; lk++) {
                //list[lk].marker.setMap(this.map);
                //list[lk].marker.setVisible(true);
                list[lk].marker = new google.maps.Marker({ map: this.map, icon: list[lk].marker.icon, position: new google.maps.LatLng(list[lk].marker.position.lat(), list[lk].marker.position.lng()), title: list[lk].marker.title, visible: true });
            }
        }


        function initializeRoutesObjects() {
            localDisplayMap.directionsDisplay = new google.maps.DirectionsRenderer();
            localDisplayMap.directionsDisplay.setOptions({ preserveViewport: true, suppressMarkers: true });

            //this.geocoder = new google.maps.Geocoder();

            localDisplayMap.directionsService = new google.maps.DirectionsService();

        }

        function GetAllBusRouteDetailsByBusRouteID() {
            PageMethods.wsGetAllBusRouteDetailsByBusRouteID(localDisplayMap.busRouteID, GetAllBusRouteDetailsByBusRouteIDOk, GetAllBusRouteDetailsByBusRouteIDError);
        }

        function GetAllBusRouteDetailsByBusRouteIDOk(object) {
            BusStopsManager.openXMLBusRouteDetailsByBusRouteID(object, localDisplayMap);

            localDisplayMap.temporalyIndex = 0;
            localDisplayMap.temporalyInterval = setInterval(function () { loadBusRouteDisplay(); }, localDisplayMap.timeToCalculateRoute);
        }

        function GetAllBusRouteDetailsByBusRouteIDError(error) {
            //responseEnd(getrpvBusStopsManagerUpdateControl());
            objWindowsMessageManager.show_Error(error.get_message(), titles.busStopsTitle);
        }

        function loadBusRouteDisplay() {
            if (localDisplayMap.temporalyIndex < localDisplayMap.temporalyList.length) {
                addBusRouteClickDB(localDisplayMap.temporalyList[localDisplayMap.temporalyIndex++]);
            }
            else {
                clearInterval(localDisplayMap.temporalyInterval);
                //responseEnd(getrpvBusStopsManagerUpdateControl());
            }
        }

        function addBusRouteClickDB(latLng) {

            var lmarker = new google.maps.Marker({
                position: new google.maps.LatLng(latLng.lat(), latLng.lng()),
                map: map,
                icon: images.locationBusRoute,
                draggable: false,
                visible: false
            });

            localDisplayMap.polygonoBusRouteMarkers.push(lmarker);

            lmarker.setTitle("#" + localDisplayMap.polygonoBusRouteMarkers.length);

            if (localDisplayMap.polygonoBusRouteMarkers.length > 1) {
                localDisplayMap.originPoint = localDisplayMap.polygonoBusRouteMarkers[localDisplayMap.polygonoBusRouteMarkers.length - 2].position;
                localDisplayMap.destinationPoint = localDisplayMap.polygonoBusRouteMarkers[localDisplayMap.polygonoBusRouteMarkers.length - 1].position;
                calculateBusStopRouteFromPointToPoint();
            }
        }

        function calculateBusStopRouteFromPointToPoint() {
            var request = {
                origin: localDisplayMap.originPoint,
                destination: localDisplayMap.destinationPoint,
                //waypoints: waypts,
                optimizeWaypoints: true,
                travelMode: google.maps.DirectionsTravelMode['DRIVING'],
                unitSystem: google.maps.DirectionsUnitSystem['METRIC'],
                provideRouteAlternatives: false
            };
            localDisplayMap.directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {

                    localDisplayMap.route = response.routes[0];

                    showDirectionsDisplay(localDisplayMap.directionsDisplay);

                    if (localDisplayMap.firstBusRoute) {
                        localDisplayMap.busRoutes.push(localDisplayMap.route);
                        localDisplayMap.firstBusRoute = false;
                    }
                    else {
                        localDisplayMap.busRoutes[0].legs.push(localDisplayMap.route.legs[0]);
                    }

                    response.routes = localDisplayMap.busRoutes;
                    localDisplayMap.response = response;

                    setDirectionsDisplay(localDisplayMap.directionsDisplay, localDisplayMap.response);
                }
                else {
                    null;
                }
            });
        }

        function setDirectionsDisplay(directionsDisplay, route) {
            directionsDisplay.setDirections(route);
            //globalBusRoutesManager.directionsDisplay.setDirections(route);
        }

        function showDirectionsDisplay(directionsDisplay) {
            directionsDisplay.setMap(this.map);
        }


    </script>
</telerik:RadScriptBlock>
</head>
<body>
    <form id="form1" runat="server">
       <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true"></telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
					<telerik:AjaxSetting AjaxControlID="AjaxControlID1">
						<UpdatedControls>
							<telerik:AjaxUpdatedControl ControlID="Loading" LoadingPanelID="RadAjaxLoadingPanel1" />
						</UpdatedControls>
					</telerik:AjaxSetting>
				</AjaxSettings>
       </telerik:RadAjaxManager>

<div id="dvMap" style="width:100%; height:100%;">
</div>
        </form>
</body>
